﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 小小次朗点餐系统
{
    public partial class Form5 : Form
    {    //添加修改的识别变量 0:添加   1：修改
        public static int type = 0;
        //记录前一画面修改的行号
        public static int index = 0;
        public static DataTable dt1 = new DataTable();
        public Form5()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*string fileName;
            using (OpenFileDialog OpenFD = new OpenFileDialog())     //实例化一个 OpenFileDialog 的对象
            {
                *//*//定义打开的默认文件夹位置
                OpenFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                OpenFD.ShowDialog();                  //显示打开本地文件的窗体
                fileName = OpenFD.FileName;             //把 路径名称 赋给 fileName
                textBox1.Text = fileName;             //将 路径名称 显示在 textBox 控件上
                pictureBox1.Load(textBox1.Text);*//*

            }*/

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox1.Text != "" && textBox2.Text != "")
            {
                int MeID = Accounts.ID;

                //添加功能实现
                if (Accounts.type == "0")
                {

                   
                    //*********************************数据库添加数据
                    string sql = string.Format($"insert into Food(FoodName,FoodPrice,FoodType,MeID) values('{textBox1.Text}','{textBox2.Text}','{textBox3.Text}','{MeID}')");
                    bool rlt = DBHelper.GetNonQuery(sql);
                    if (rlt)
                    {
                        MessageBox.Show("添加成功");
                        //*********************************画面控件添加数据
                        //声名表的新行
                        DataRow dr = Form6.dt.NewRow();

                        //-------行内用户编号列设定***********（应为是数据库自增列 所以需要从数据库中查找出来再设定）
                        string sql1 = string.Format($"select FoodID from Food where FoodName='{textBox1.Text}'");
                        DataSet ds = DBHelper.SelectByAdapter(sql1);
                        DataTable dt = ds.Tables[0];
                        dr["FoodID"] = dt.Rows[0]["FoodID"].ToString();

                        //-------行内账号列设定
                        dr["FoodName"] = textBox1.Text;

                        //-------行内用户名列设定
                        dr["FoodPrice"] = textBox2.Text;

                        //-------行内用户类型列设定
                        dr["FoodType"] = textBox3.Text;

                        //添加到画面控件数据源表中
                        Form6.dt.Rows.Add(dr);
                    }
                }
                //修改功能实现
                if (Accounts.type == "1")
                {
                    //********************数据库数据更新
                    //取得所选行的用户编号----作为更改的定位条件
                    string id = Form6.dt.Rows[index]["FoodID"].ToString();
                    string sql = string.Format($"update Food set FoodName='{textBox1.Text}',FoodPrice='{textBox2.Text}',FoodType='{textBox3.Text}' where FoodID={id}");
                    bool rlt = DBHelper.GetNonQuery(sql);
                    if (rlt)
                    {
                        MessageBox.Show("修改成功");
                        //********************画面控件数据更新
                        //取得画面选中行
                        DataRow dr = Form6.dt.Rows[index];
                        //开始编辑
                        dr.BeginEdit();
                        dr["FoodName"] = textBox1.Text;
                        dr["FoodPrice"] = textBox2.Text;
                        dr["FoodType"] = textBox3.Text;
                        dr.EndEdit();
                    }

                }

            }
            else
            {
                MessageBox.Show("画面数据填写不完整！");
            }
            

            }

        private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void Form5_Load(object sender, EventArgs e)
        {
            //修改功能实现前

            if (Accounts.type == "1")
            {
                
                //取得所选行的菜单表中的菜单ID FoodID
                string id = Form6.dt.Rows[index]["FoodID"].ToString();


                //用取得的FoodID进行sql查询
                string sql = string.Format($"select * from Food where FoodID='{id}'");
                DataSet ds = DBHelper.SelectByAdapter(sql);
                DataTable dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    //用查出的数据赋值给相应控件，dt.Rows[0][3]意思是查询出来的第一行数据的第3个列的值。

                    
                    textBox1.Text = dt.Rows[0][2].ToString();
                    textBox2.Text = dt.Rows[0][3].ToString();
                    textBox3.Text = dt.Rows[0][1].ToString();
                }
            }

            if (Accounts.type == "2")
            {
                panel2.Visible = true;

                string sql = string.Format($"select * from Seller where Senumber='{Accounts.accounts}'");
                DataSet ds = DBHelper.SelectByAdapter(sql);
                dt1 = ds.Tables[0];
                if (dt1.Rows.Count > 0)
                {
                    //加载字段

                    textBox4.Text = dt1.Rows[0][2].ToString();
                    textBox5.Text = dt1.Rows[0][4].ToString();
                }
            }

           

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (Accounts.type == "2")
            {
                string sql = string.Format($"update Seller set Name='{textBox4.Text}',phone='{textBox5.Text}' where Senumber='{Accounts.accounts}'");
                bool rlt = DBHelper.GetNonQuery(sql);
                if (rlt)
                {
                    MessageBox.Show("修改成功");
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
        }
    }
}
